package mygame;

import main.GameTree;
import javax.swing.ImageIcon;

import player.Bullit;
import player.Player;
import player.Gun;
import player.Reticle;
import player.Shot;
import player.Triangle;
import utilPackage.Star;

import main.TAdapter;
import quadtree.DoublePoint;

import stateManager.State;
import stateManager.StateManager;

public class State1 extends State {

	protected Player player, player2;
	protected Triangle square1, square2;
	protected Triangle square3;
	protected Triangle square4;
	protected Triangle square5;
	protected Triangle square6;
	protected Triangle square7;
	protected Triangle square8;
	protected Triangle square9;
	protected Triangle square10;
	protected Gun testGun;
	protected Reticle testReticle;
	public State1(){
		stateManager = StateManager.getInstance();		
	}
	
	public GameTree modifyGameTree(){
		gameTree = new GameTree();
		square3 = new Triangle(150, 500, 50, 50, 2);
		square3.setGameTree(gameTree);
		square4 = new Triangle(450, 500, 50, 50, -2);
		square4.setGameTree(gameTree);
		gameTree.insertSprite(new DoublePoint(150, 500), square3);
		gameTree.insertSprite(new DoublePoint(450, 500), square4);
		testGun = new Gun();
		testReticle = new Reticle();
		gameTree.insertSprite(new DoublePoint(testReticle.getX(), testReticle.getY()), testReticle);
		gameTree.insertSprite(new DoublePoint(testGun.getX(), testGun.getY()), testGun);
		return gameTree;
	}
	
	
	public GameTree gameHandleInput(TAdapter userInput){
		if(userInput.inputHandler.DOWN_PUSH == true){
			//player.keyPressed("VK_DOWN");
			

		}
		if(userInput.inputHandler.UP_PUSH == true){
			player.keyPressed("VK_UP");

		}
		if(userInput.inputHandler.RIGHT_PUSH == true){
			player.keyPressed("VK_RIGHT");

		}	
		if(userInput.inputHandler.LEFT_PUSH == true){
			player.keyPressed("VK_LEFT");	
		}
		if(userInput.inputHandler.S_PUSH == true){
			player2.keyPressed("VK_DOWN");

		}
		if(userInput.inputHandler.W_PUSH == true){
			player2.keyPressed("VK_UP");

		}
		if(userInput.inputHandler.D_PUSH == true){
			player2.keyPressed("VK_RIGHT");

		}	
		if(userInput.inputHandler.A_PUSH == true){
			player2.keyPressed("VK_LEFT");	
		}
		if(userInput.inputHandler.SPACE_PUSH == true){
			player.keyPressed("VK_SPACE");
			Shot s = new Shot(player.getX(),player.getY());
			gameTree.insertSprite(new DoublePoint(player.getX(), player.getY() ),s  );	
		}
		if(userInput.inputHandler.SHIFT_PUSH == true){
			player.keyPressed("VK_SHIFT");

		}
		if(userInput.inputHandler.DOWN_RELEASE == true){
			//player.keyReleased("VK_DOWN");
			userInput.inputHandler.DOWN_RELEASE = false;

		}
		if(userInput.inputHandler.UP_RELEASE == true){
			player.keyReleased("VK_UP");
			userInput.inputHandler.UP_RELEASE = false;
		}
		if(userInput.inputHandler.RIGHT_RELEASE== true){
			player.keyReleased("VK_RIGHT");
			userInput.inputHandler.RIGHT_RELEASE = false;
		}	
		if(userInput.inputHandler.LEFT_RELEASE == true){
			player.keyReleased("VK_LEFT");	
			userInput.inputHandler.LEFT_RELEASE = false;
		}
		if(userInput.inputHandler.DOWN_RELEASE == true){
			player.keyReleased("VK_DOWN");
			userInput.inputHandler.DOWN_RELEASE = false;

		}
		if(userInput.inputHandler.W_RELEASE == true){
			player2.keyReleased("VK_UP");
			userInput.inputHandler.W_RELEASE = false;
		}
		if(userInput.inputHandler.D_RELEASE== true){
			player2.keyReleased("VK_RIGHT");
			userInput.inputHandler.D_RELEASE = false;
		}	
		if(userInput.inputHandler.A_RELEASE == true){
			player2.keyReleased("VK_LEFT");	
			userInput.inputHandler.A_RELEASE = false;
		}
		if(userInput.inputHandler.S_RELEASE == true){
			player2.keyReleased("VK_DOWN");
			userInput.inputHandler.S_RELEASE = false;

		}
		if(userInput.inputHandler.SPACE_RELEASE == true){
			player.keyReleased("VK_SPACE");
			

		}
		if(userInput.inputHandler.SHIFT_RELEASE == true){
			player.keyReleased("VK_SHIFT");
		}
		if(userInput.inputHandler.MOUSE_MOVED == true) {
			userInput.inputHandler.MOUSE_MOVED = false;
			testReticle.setX(userInput.MouseX);
			testReticle.setY(userInput.MouseY);
			mouseMoved(userInput.MouseX, userInput.MouseY);
		}
		if(userInput.inputHandler.MOUSE_PRESSED == true) {
			userInput.inputHandler.MOUSE_PRESSED = false;
			//stateManager.changeStatesOnNextLoop(this, "defaultToEmpty");
			if(userInput.inputHandler.MOUSE_LEFT_CLICK) {
				userInput.inputHandler.MOUSE_LEFT_CLICK = false;
				mousePressed(userInput.MouseX, userInput.MouseY);
				testReticle.setWidth(5);
			}
			else if(userInput.inputHandler.MOUSE_RIGHT_CLICK) {
				userInput.inputHandler.MOUSE_RIGHT_CLICK = false;
				Triangle tmp = new Triangle(userInput.MouseX, userInput.MouseY, 50, 50, -2);
				tmp.setGameTree(gameTree);
				gameTree.insertSprite(new DoublePoint(userInput.MouseX, userInput.MouseY), 
						tmp);
				tmp = null;
			}
		}
		return gameTree;
	}


	public GameTree handleInput(TAdapter userInput){
		try{
			gameHandleInput(userInput);
			return gameTree;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return gameTree;
	}
	
	public void mousePressed(int X, int Y) {
		//super.mouseClicked(e);
		// TODO Auto-generated method stub
		int offset = 75;
		int x = X-offset;
		int y = Y-offset;
		
		Bullit tmp = new Bullit(testGun.getXChange() + testGun.getX(), testGun.getYChange() + testGun.getY(), 
				testGun.getXDirection() * (int) (5 * Math.cos(testGun.getRadians())), (int) (5 * Math.sin(testGun.getRadians())));
		gameTree.insertSprite(new DoublePoint(testGun.getX(), testGun.getY()), 
				tmp);
		tmp = null;
		//MouseClickQueryBox
		/*Vector<PointNodeElement<Sprite>> checkElements;
		checkElements = gameTree.spritesTree.queryObject(new Point(x, y), 
				new Dimension(offset*2,offset*2));
		for(int i = 0; i < checkElements.size(); i++) {
			try {
				Triangle spriter = (Triangle)checkElements.get(i).getElement();
				if(spriter.pointWithin(x+offset, y+offset)) {
					((Triangle)spriter).die();
				}
			} catch (Exception exc) {
				//do nothing
			}
		}*/
	}
	
	public void mouseMoved(int X, int Y) {
		// TODO Auto-generated method stub
		testGun.setEndX(X);
		testGun.setEndY(Y);
	}
	
	
	
	
}
